package 寒假刷题;

import java.util.LinkedList;
import java.util.List;

public class 组合 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> lists = new LinkedList<>();
        List<Integer> list = new LinkedList<>();
        dfs(n,k,lists,list,1);
        return lists;
    }

    private void dfs(int n, int k, List<List<Integer>> lists, List<Integer> list, int i) {
        if (list.size()==k)
            lists.add(new LinkedList<>(list));
        for (int j = i; j <=n ; j++) {
            list.add(j);
            dfs(n,k,lists,list,j+1);
            list.remove(list.size()-1);
        }
    }
}
